package com.treeroot.userservice.user.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.treeroot.commonutils.AOP.Limit;
import com.treeroot.commonutils.ordervo.UserInfoOrder;
import com.treeroot.commonutils.result.R;
import com.treeroot.commonutils.utils.JwtUtils;
import com.treeroot.commonutils.utils.MD5;
import com.treeroot.servicebase.config.ApiVersion;
import com.treeroot.userservice.user.entity.PcUserInfo;
import com.treeroot.userservice.user.entity.vo.RegisterVo;
import com.treeroot.userservice.user.service.PcUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TreeRoot
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/user/pcuserinfo")
@Api(description = "用户管理")
@Slf4j
@CrossOrigin
@ApiVersion(1.0)
public class PcUserInfoController {

    @Autowired
    private PcUserInfoService userInfoService;


    /**
     * echarts
     */
    @GetMapping("count")
    public R count() {
        return R.ok().data("userCount", userInfoService.count(null));
    }


    /**
     * 用户登录
     *
     * @param userInfo 用户实体类 信息
     */
    @PostMapping("login")
    @Limit(key = "login", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    @HystrixCommand(fallbackMethod = "loginFallback")
    public R login(@RequestBody PcUserInfo userInfo) {
        log.info("令牌桶count获取令牌成功");
        String token = userInfoService.login(userInfo);
        return R.ok().data("token", token);
    }

    public R loginFallback(@RequestBody PcUserInfo userInfo) {
        return R.error().message("操作异常/系统异常");

    }

    /**
     * 用户注册
     *
     * @param registerVo 用户实体类 信息
     */
    @PostMapping("register")
    @Limit(key = "register", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    @HystrixCommand(fallbackMethod = "registerFallback")
    public R register(@RequestBody RegisterVo registerVo) {
        userInfoService.register(registerVo);
        return R.ok().message("注册成功");
    }

    public R registerFallback(@RequestBody RegisterVo registerVo) {
        return R.error().message("操作异常/系统异常");

    }

    /**
     * 根据request对象获取头部信息，返回用户id
     * 通过用户id得到用户信息  返回 用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("getUserInfoByToken")
    @Limit(key = "getPlaneCondition", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    @HystrixCommand(fallbackMethod = "getUserInfoByTokenFallback")
    public R getUserInfoByToken(HttpServletRequest request) {
        // 调用jwt工具类。根据request对象获取头部信息，返回用户id
        //        调数据库 根据用户id获取用户信息
        PcUserInfo user = userInfoService.getById(JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("userInfo", user);
    }

    public R getUserInfoByTokenFallback(HttpServletRequest request) {
        return R.error().message("操作异常/系统异常");

    }


    /**
     * 根据ID查询用户
     *
     * @param id
     */

    @ApiOperation(value = "根据ID获取用户信息")
    @GetMapping("getUser/{id}")
    public R getUserById(
            @PathVariable String id) {
        return R.ok().data("user", userInfoService.getById(id));
    }

    /**
     * 远程  根据用户id获取用户信息
     *
     * @param id 用户id
     * @return
     */

    @PostMapping("getUserInfoOrder/{id}")
    @Limit(key = "getUserInfoOrder", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    @HystrixCommand(fallbackMethod = "getUserInfoOrderFallback")

    public UserInfoOrder getUserInfoOrder(@PathVariable String id) {
        UserInfoOrder userInfoOrder = userInfoService.getUserOrder(id);
        return userInfoOrder;
    }

    public R getUserInfoOrderFallback(@PathVariable String id) {
        return R.error().message("操作异常/系统异常");

    }


    /**
     * 新增用户
     *
     * @param user
     */


    @ApiOperation(" 添加用户信息")
    @PostMapping("addUser")
    public R addUser(@RequestBody PcUserInfo user) {
        //        密码不加密问题
        user.setPassword(MD5.encrypt(user.getPassword()));
        userInfoService.save(user);
        return R.ok();
    }

    /**
     * @param id 用户 id
     */
    @ApiOperation(value = "根据ID删除用户")
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id) {

        userInfoService.removeById(id);

        return R.ok();
    }

    /**
     * 修改用户信息
     *
     * @param user 用户信息 实体类
     */
    @ApiOperation(value = "修改用户信息")
    @PutMapping("updateUser")

    public R updateUser(@RequestBody PcUserInfo user) {

        //        密码不加密问题
        user.setPassword(MD5.encrypt(user.getPassword()));
        userInfoService.updateById(user);
        return R.ok();
    }


    /**
     * 条件查询带分页的方法
     *
     * @param current 当前页
     * @param limit   最大页
     * @param user    需要的数据
     */
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageUserCondition/{current}/{limit}")
    public R pageUserCondition(@PathVariable long current, @PathVariable long limit,
                               @RequestBody(required = false) PcUserInfo user) {
        Map map = userInfoService.getQueryUserList(current, limit, user);
        return R.ok().data(map);

    }


}


